package qlsl.androiddesign.util.commonutil;

import android.graphics.Rect;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

/**
 * 虚拟按键处理类<br/>
 *
 */
public class ScreenKeyUtils {

	public static void assistActivity(View content) {
		new ScreenKeyUtils(content);
	}

	private View mChildOfContent;
	private int usableHeightPrevious;
	private ViewGroup.LayoutParams frameLayoutParams;

	private ScreenKeyUtils(View content) {
		mChildOfContent = content;
		mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
			public void onGlobalLayout() {
				possiblyResizeChildOfContent();
			}
		});
		frameLayoutParams = mChildOfContent.getLayoutParams();
	}

	private void possiblyResizeChildOfContent() {
		int usableHeightNow = computeUsableHeight();
		if (usableHeightNow != usableHeightPrevious) {
			// 如果两次高度不一致

			// int usableHeightSansKeyboard =
			// mChildOfContent.getRootView().getHeight();
			// int heightDifference = usableHeightSansKeyboard -
			// usableHeightNow;
			// if (heightDifference > (usableHeightSansKeyboard / 4)) {
			// // keyboard probably just became visible
			// isKeyBordVisiable=true;
			// } else {
			// // keyboard probably just became hidden
			// isKeyBordVisiable=false;
			// }
			// 将计算的可视高度设置成视图的高度
			frameLayoutParams.height = usableHeightNow;
			mChildOfContent.requestLayout();// 请求重新布局
			usableHeightPrevious = usableHeightNow;
		}
	}

	private int computeUsableHeight() {
		// 计算视图可视高度
		Rect r = new Rect();
		mChildOfContent.getWindowVisibleDisplayFrame(r);
		return (r.bottom - r.top);
	}
}
